package byggeTegner;

import context.*;

import model2D.Model2D;
import object2D.Object2D;

import java.awt.Cursor;
import java.awt.event.InputEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JTree;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;
import javax.swing.tree.DefaultMutableTreeNode;

// ********************************************************************
// *** CLASS: TableView
// ********************************************************************
public class TreeView  extends JTree implements Context.Constants
{ private static final boolean DEBUG=true; // Overriding Context.Constants.DEBUG
  static final String TAG="mainFrame.TableView";
  static final long serialVersionUID=0; // AD'HOC
  private static final Cursor defaultCursor=Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR);
  private static final Cursor handCursor=Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);

  private TreeModel objectTreeModel;

  // ******************************************************************
  // *** CONSTRUCTOR: TableView
  // ******************************************************************
  public TreeView(Model2D objectModel)
  { super();
    this.objectTreeModel=objectModel.toObjectTreeModel();
    this.setModel(objectTreeModel);
    this.setShowsRootHandles(true);
    //this.setEditable(true);
    this.addMouseListener(new TreeMouseAdapter());
    //this.setRowHeight(20);
  }

  
  // ******************************************************************
  // *** INNER CLASS: TreeMouseAdapter
  // ******************************************************************
  private class TreeMouseAdapter extends MouseAdapter
  { private TreePath startPath;
	  
    // ******************************************************************
    // *** METHOD: mousePressed
    // ******************************************************************
    // Invoked when a mouse button has been pressed.
	public void mousePressed(MouseEvent e)
	{ int row=getRowForLocation(e.getX(),e.getY());
      startPath=getPathForLocation(e.getX(),e.getY());
      if(startPath!=null)
      { DefaultMutableTreeNode node=(DefaultMutableTreeNode)(startPath.getLastPathComponent());
        Object2D obj=(Object2D)node.getUserObject();
	    int flg=e.getModifiers();
	    if((flg& InputEvent.BUTTON1_MASK)!=0)
	         mouseLeftPressed(row,obj,startPath);
	    else mouseRightPressed(row,obj,startPath);
      }
	}
    private void mouseLeftPressed(int row,Object2D obj,TreePath path)
    { if(DEBUG) Log.d(TAG,"MOUSE LEFT PRESSED: row="+row+", obj="+obj+", path="+path);
      ByggeTegner.getCurrentDrawPanel().setSelected(obj);
      setCursor(handCursor); repaint();
      // ... ...
    }  
    private void mouseRightPressed(int row,Object2D obj,TreePath path)
    { if(DEBUG) Log.d(TAG,"MOUSE RIGHT PRESSED: row="+row+", obj="+obj+", path="+path);
      obj.edit();
      // ... ...
    }

    // ******************************************************************
    // *** METHOD: mouseReleased
    // ******************************************************************
    // Invoked when a mouse button has been released.
    public void mouseReleased(MouseEvent e)
	{ int row=getRowForLocation(e.getX(),e.getY());
      TreePath path=getPathForLocation(e.getX(),e.getY());
      Object obj=(path==null)?null:path.getLastPathComponent();
	  int flg=e.getModifiers();
	  if((flg& InputEvent.BUTTON1_MASK)!=0)
	       mouseLeftReleased(row,obj,path);
	  else mouseRightReleased(row,obj,path);
	}
    private void mouseLeftReleased(int row,Object obj,TreePath path)
    { if(DEBUG) Log.d(TAG,"MOUSE LEFT RELEASED: row="+row+", obj="+obj+", startPath="+startPath+", path="+path);
      setCursor(defaultCursor); repaint();
      // ... ...
    }  
    private void mouseRightReleased(int row,Object obj,TreePath path)
    { if(DEBUG) Log.d(TAG,"MOUSE RIGHT RELEASED: row="+row+", obj="+obj+", path="+path);
      // ... ...
    }
 
  }

}